package com.dhecp.project.system.service.impl;

import com.dhecp.project.system.domain.SysApiLog;
import com.dhecp.project.system.mapper.SysApiLogMapper;
import com.dhecp.project.system.service.SysApiLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName SysApiLogServiceImpl
 * @Description TODO
 * @Author alula
 * @Date 2021-05-26 9:44
 */
@Service
public class SysApiLogServiceImpl implements SysApiLogService {

    private static final Logger logger = LoggerFactory.getLogger(SysApiLogServiceImpl.class);

    @Autowired
    private SysApiLogMapper sysApiLogMapper;

    @Override
    public List<SysApiLog> queryApiLogList(Map<String, Object> params) {
        return sysApiLogMapper.findApiLogList(params);
    }

    @Override
    public SysApiLog queryApiLogById(int id) {
        return  sysApiLogMapper.findApiLogById(id);
    }

    @Override
    public int addApiLog(String system, String type, String input, String output, int code, String message) {
        int result = 0;
        try {
            String guid = UUID.randomUUID().toString().toUpperCase();
            SysApiLog apiLog = new SysApiLog();
            apiLog.setSystem(system);
            apiLog.setType(type);
            apiLog.setInput(input);
            apiLog.setOutput(output);
            apiLog.setCode(code);
            apiLog.setMessage(message);
            sysApiLogMapper.insert(apiLog);
            result = apiLog.getId();
        }catch (Exception ex){
            logger.error("调用API接口保存日志出错，错误信息为：" + ex.getMessage());
        }
        return result;
    }

    @Override
    public void updateApiLog(int id,String system, String type, String input, String output, int code, String message) {
        try{
            SysApiLog apiLog = sysApiLogMapper.findApiLogById(id);
            if(apiLog != null){
                apiLog.setOutput(output);
                apiLog.setCode(code);
                apiLog.setMessage(message);
                sysApiLogMapper.update(apiLog);
            }else{
                addApiLog(system, type, input, output, code, message);
            }
        }catch (Exception ex){
            logger.error("调用API接口更新日志出错，错误信息为：" + ex.getMessage());
        }
    }
}
